package com.foreknow.elm.controller;

import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.service.impl.OrdersServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class OrdersController {
    /**
     * 创建订单
     * http://localhost:8080/elm/OrdersController/createOrders?userId=12345671111&businessId=10001&daId=1&orderTotal=99
     * @param request
     * @return Object
     */
    public Object createOrders(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        int businessId = Integer.parseInt(request.getParameter("businessId"));
        int daId = Integer.parseInt(request.getParameter("daId"));
        double orderTotal = Double.parseDouble(request.getParameter("orderTotal"));
        //调用Service中的方法
        OrdersService ordersService = new OrdersServiceImpl();
        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setBusinessId(businessId);
        orders.setDaId(daId);
        orders.setOrderTotal(orderTotal);
        int orderId = ordersService.createOrders(orders);
        return orderId;
    }

    public Object getOrdersById(HttpServletRequest request) {
        Integer orderId = Integer.valueOf(request.getParameter("orderId"));
        OrdersService service = new OrdersServiceImpl();
        Orders orders = service.getOrdersById(orderId);
        return orders;
    }

    public Object listOrdersByUserId(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        OrdersService service = new OrdersServiceImpl();
        List<Orders> list = service.listOrdersByUserId(userId);
        return list;
    }
}
